package com.bocom.hairy.crab.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bocom.hairy.crab.base.common.ResultDate;
import com.bocom.hairy.crab.base.entity.User;
import com.bocom.hairy.crab.base.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName com.bocom.hairy.crab.web.controller.UserController <br/>
 * @Description: 用户控制器          <br/>
 * @Author jiangshikun             <br/>
 * @Date 2025/9/3 下午1:46         <br/>
 * @Version V1.0
 * ----------------------------------------
 * （^-^）心即理，知行合--致良知 （^-^）
 * 邮 箱:duzhuai@live.cn
 * 地 址：北京市海淀区永澄北路2号
 **/
@Tag(name = "用户管理")
@RestController
@RequestMapping("/users")
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据ID获取用户
     */
    @Operation(summary = "根据ID获取用户")
    @GetMapping("/{userId}")
    public ResultDate<User> getById(@PathVariable String userId) {
        User user = userService.getById(userId);
        return ResultDate.ok(user);
    }

    /**
     * 分页查询用户
     */
    @Operation(summary = "分页查询用户")
    @GetMapping("/page")
    public ResultDate<IPage<User>> getPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            User user) {
        IPage<User> userPage = userService.getUserPage(page, size, user);
        return ResultDate.ok(userPage);
    }

    /**
     * 添加用户
     */
    @Operation(summary = "添加用户")
    @PostMapping
    public ResultDate<Boolean> save(@Valid @RequestBody User user) {
        boolean result = userService.save(user);
        return ResultDate.ok(result);
    }

    /**
     * 更新用户
     */
    @Operation(summary = "更新用户")
    @PutMapping
    public ResultDate<Boolean> update(@Valid @RequestBody User user) {
        boolean result = userService.updateById(user);
        return ResultDate.ok(result);
    }

    /**
     * 删除用户
     */
    @Operation(summary = "删除用户")
    @DeleteMapping("/{userId}")
    public ResultDate<Boolean> remove(@PathVariable String userId) {
        boolean result = userService.removeById(userId);
        return ResultDate.ok(result);
    }

    /**
     * 批量添加用户
     */
    @Operation(summary = "批量添加用户")
    @PostMapping("/batch")
    public ResultDate<Boolean> saveBatch(@Valid @RequestBody List<User> userList) {
        boolean result = userService.saveBatch(userList);
        return ResultDate.ok(result);
    }

    /**
     * 批量更新用户
     */
    @Operation(summary = "批量更新用户")
    @PutMapping("/batch")
    public ResultDate<Boolean> updateBatch(@Valid @RequestBody List<User> userList) {
        boolean result = userService.updateBatch(userList);
        return ResultDate.ok(result);
    }

    /**
     * 批量删除用户
     */
    @Operation(summary = "批量删除用户")
    @DeleteMapping("/batch")
    public ResultDate<Boolean> removeBatch(@RequestBody List<String> userIds) {
        boolean result = userService.removeBatchByIds(userIds);
        return ResultDate.ok(result);
    }
}